package com.xidian.zhiancloud.service.equipFacility;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.PreUnitMapper;
import com.xidian.zhiancloud.dao.PreUnitTypeMapper;
import com.xidian.zhiancloud.dao.PuOutMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.EmergencyMaterialsInventoryManagement;
import com.xidian.zhiancloud.model.PreUnit;
import com.xidian.zhiancloud.model.PreUnitType;
import com.xidian.zhiancloud.model.PuOut;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.equipFacility.PuOutQO;
import com.xidian.zhiancloud.vo.equipFacility.PuInVO;
import com.xidian.zhiancloud.vo.equipFacility.PuOutVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PuOutService extends ServiceImpl<PuOutMapper, PuOut> implements IService<PuOut> {

    @Autowired
    private PuOutMapper puOutMapper;
    @Autowired
    private PreUnitMapper preUnitMapper;
    @Autowired
    private PreUnitTypeMapper preUnitTypeMapper;
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    public Boolean add(PuOut puOut) {
        int result = puOutMapper.insert(puOut);
        if (result == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }
        Integer numOut=puOut.getReceivingNum();
        Long puId=puOut.getPuId();
        PreUnit preUnit=preUnitMapper.selectById(puId);
        Integer num=preUnit.getNum();
        preUnit.setNum(num-numOut);
        int i = preUnitMapper.updateById(preUnit);
        return i==1;
    }

    public IPage<PuOutVO> getPuOut(PuOutQO puOutQO) {
        Page<PuOutVO> page=new Page<>(puOutQO.getCurrent(), puOutQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("chu_time").setAsc(false);
        page.getOrders().add(orderItem);
        IPage<PuOutVO> ans=puOutMapper.selectByQO(page,puOutQO);
        for (PuOutVO record : ans.getRecords()) {
            PreUnit preUnit=preUnitMapper.selectById(record.getPuId());
            //record.setPuCode(preUnit==null?"该备件不存在":preUnit.getPuCode());
            record.setPuName(preUnit==null?"该备件不存在":preUnit.getName());
            PreUnitType preUnitType=preUnitTypeMapper.selectById(preUnit.getPreUnitTypeId());
            record.setPerUnitTypeName(preUnitType==null?"该备件类型不存在":preUnitType.getName());
            record.setModel(preUnit==null?"该备件不存在":preUnit.getModel());
            Organization org = organizationMapper.selectById(preUnit.getMngCtrDeptId());
            record.setMngCtrDeptName(org==null?"该部门不存在":org.getName());
            Organization org2=organizationMapper.selectById(record.getReceivingDeptId());
            record.setReceivingDeptName(org2==null?"该部门不存在":org2.getName());
            SysUser userid =sysUserMapper.selectById(record.getRecipientId());
            record.setRecipientName(userid==null?"该用户不存在":userid.getName());
            SysUser userid2 =sysUserMapper.selectById(record.getChuer());
            record.setCreator(userid2==null?"该用户不存在":userid2.getName());
        }
        return ans;

    }
}
